import { _decorator, Component, Node, PhysicsSystem, Vec3, Scene, director, Camera, v3 } from 'cc';
import { Constant } from '../Common/Constant';
import StorageManager from '../Common/StorageManager';
import { CameraController } from '../Controller/CameraController';
import { EasyController, EasyControllerEvent } from '../Controller/EasyController';
import { PlayerController } from '../Controller/PlayerController';
import { PropController, PropType } from '../Controller/PropController';
import { PropData } from '../Data/PropData';
import { BloodEffect } from '../UI/BloodEffect';
import { UIManager } from './UIManager';
const { ccclass, property } = _decorator;

@ccclass('GameManager')
export class GameManager extends Component {

    static Instance: GameManager = null;

    @property(PlayerController)
    player: PlayerController = null!;

    @property(Camera)
    playerCamera: Camera = null!;

    @property(Node)
    Enemy: Node;

    propDatas: Array<PropData> = new Array();

    scene: Scene = null!;

    __preload() {
        //PhysicsSystem.instance.gravity = new Vec3(0, -30, 0);
    }

    onLoad() {
        GameManager.Instance = this;

        this.scene = director.getScene();

        // this.aiNds[0].getComponent(PropController).propType = PropType.Battery;
        // this.aiNds[1].getComponent(PropController).propType = PropType.Bandage;
        // this.aiNds[2].getComponent(PropController).propType = PropType.Food;
        // this.aiNds[3].getComponent(PropController).propType = PropType.Food;
        // this.aiNds[4].getComponent(PropController).propType = PropType.Weapon;
    }

    start() {
    }

    AddProp(propType: PropType) {
        console.log(`获得道具：${propType}`);
        let data = this.propDatas.find(propData => propData.propType == propType);
        if (!data) {
            this.propDatas.push(new PropData(propType, 1));
        } else {
            data.count += 1;
        }

        GameManager.Instance.scene.emit(EasyControllerEvent.REFRESH_PROP);
    }

    UseProp(propType: PropType) {
        console.log(`使用道具：${propType}`);
        let data = this.propDatas.find(propData => propData.propType == propType);
        if (!data) return;
        if (data.count <= 0) {
            UIManager.ShowTip(`道具不足。`);
        }

        switch (data.propType) {
            case PropType.Battery:
                break;

            case PropType.Bandage:
                break;

            case PropType.Food:
                break;

            case PropType.Weapon:
                break;

        }

        data.count -= 1;

        // if (data.count == 0) {
        //     this.propDatas = this.propDatas.filter(propData => propData !== data);
        // }

        GameManager.Instance.scene.emit(EasyControllerEvent.REFRESH_PROP);
    }

    dir = v3();


    testbool: boolean = false;
    OnCameraZoom() {
        //Vec3.subtract(this.dir, this.Enemy.worldPosition, this.player.node.worldPosition);
        //this.playerCamera.getComponent(CameraController).SetTargetAngle(this.Enemy.worldPosition.x, this.Enemy.worldPosition.y, this.Enemy.worldPosition.z);

        //BloodEffect.Instance.Show();
        this.testbool = !this.testbool;
        this.player.IsRun(this.testbool);
    }

    update(deltaTime: number) {
    }

    onDestroy() {
    }
}


